﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyNhaThuoc.Models;
using QuanLyNhaThuoc.HelperClass;
using System.Security.Cryptography;
using System.Configuration;

namespace QuanLyNhaThuoc.Controllers
{
    public class AccountController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region Check function permission
            string controller = RouteData.Values["controller"].ToString();
            string action = RouteData.Values["action"].ToString();

            var query = PMOEn.CheckFunctionPermission("", controller, action).ToList();
            if (query.Count != 0)
            {
                ViewBag.Title = query[0].Title;
            }
            else
            {
                Response.Redirect("~/Error/Denied");
            }
            #endregion

            base.OnActionExecuting(filterContext);
        }

        //
        // GET: /Account/

        PMOEntities PMOEn = new PMOEntities();

        public IFunctionService Function { get; set; }

        public ActionResult LogIn()
        {

            return View();
        }

        [HttpPost]
        public ActionResult LoginAction(AccountModel model, string url, FormCollection frm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //Check UserName and Password

                    string password = Sercurity.HashPassword(frm[1], ConfigurationManager.AppSettings["saltValue"], HashAlgorithm.Create("SHA512"));
                    var item = PMOEn.Login(frm[0], password).ToList();

                    if (item.Count != 0)
                    {
                        //Put user data into Session

                        Session["USERID"] = item[0].UserID;
                        Session["USERNAME"] = item[0].UserName;
                        Session["BRANCHID"] = item[0].BranchID;
                        Session["CORPID"] = item[0].CorpID;
                        Session["FULLNAME"] = item[0].FullName;
                        Session["CORPNAME"] = item[0].CorpName;
                        Session["DATECREATED"] = item[0].DateCreated;

                        if (item[0].Avatar == "" || item[0].Avatar == null)
                        {
                            if (item[0].Gender == "M")
                            {
                                Session["AVATAR"] = "avatarMale.png";
                            }
                            else
                            {
                                Session["AVATAR"] = "avatarFMale.png";
                            }
                        }
                        else
                        {
                            Session["AVATAR"] = item[0].Avatar;
                        }

                        if (Url.IsLocalUrl(url))
                        {
                            return Redirect(url);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Sai tên Đăng nhập hoặc Mật khẩu");
                    }

                }
                else
                {
                    ModelState.AddModelError("", "Sai tên Đăng nhập hoặc Mật khẩu");
                }

                return View(model);
            }
            catch (Exception ex)
            {
                return RedirectToAction("Index", "Error");
            }
        }

        public ActionResult LogOut()
        {
            Session["USERID"] = null;            
            return RedirectToAction("LogIn", "Account");
        }

    }
}
